package com.wx.login

import android.app.Activity
import android.content.Context
import android.widget.Toast
import com.tencent.mm.opensdk.modelmsg.*
import com.tencent.mm.opensdk.openapi.IWXAPI
import com.tencent.mm.opensdk.openapi.WXAPIFactory

object WXUtils {
    /**
     * 微信登录相关
     */
    lateinit var iwxapi: IWXAPI

    /**
     * 将应用的appid注册到微信
     *
     * @param context
     * @param wxAppId
     */
    @JvmStatic
    fun initWxApi(context: Context, wxAppId: String) {
        iwxapi = WXAPIFactory.createWXAPI(context, wxAppId, true)
        iwxapi.registerApp(wxAppId)
    }

    /**
     * 微信登录
     *
     * @param context 上下文对象
     */
    @JvmStatic
    fun loginWx(context: Context) {
        if (!iwxapi.isWXAppInstalled) {
            Toast.makeText(context, "请先安装微信", Toast.LENGTH_SHORT).show()
            return
        }
        val req = SendAuth.Req()
        req.scope = "snsapi_userinfo"
        req.state = "wechat_sdk_login"
        iwxapi.sendReq(req)
    }

    /**
     * 微信分享
     *
     * @param friendsCircle true:朋友圈
     * @param shareText     分享内容
     */
    fun shareText(activity: Activity, friendsCircle: Boolean, shareText: String?) {
        val context = activity.applicationContext
        if (!iwxapi.isWXAppInstalled) {
            Toast.makeText(context, "请先安装微信", Toast.LENGTH_SHORT).show()
            return
        }
        if (shareText == null) {
            Toast.makeText(context, "分享失败", Toast.LENGTH_SHORT).show()
            return
        }
        Thread {
            activity.runOnUiThread {
                val textObj = WXTextObject()
                textObj.text = shareText
                val msg = WXMediaMessage()
                msg.mediaObject = textObj
                msg.description = textObj.text
                val req = SendMessageToWX.Req()
                req.transaction = buildTransaction("text")
                req.message = msg
                req.scene = if (friendsCircle) SendMessageToWX.Req.WXSceneTimeline else SendMessageToWX.Req.WXSceneSession
                iwxapi.sendReq(req)
            }
        }.start()
    }

    /**
     * 微信分享
     *
     * @param friendsCircle true:朋友圈
     * @param title         分享标题
     * @param desc          分享描述
     * @param shareUrl      分享链接
     */
    fun shareWebPage(activity: Activity, friendsCircle: Boolean, title: String?, desc: String?, shareUrl: String?) {
        val context = activity.applicationContext
        if (!iwxapi.isWXAppInstalled) {
            Toast.makeText(context, "请先安装微信", Toast.LENGTH_SHORT).show()
            return
        }
        if (shareUrl == null) {
            Toast.makeText(context, "分享失败", Toast.LENGTH_SHORT).show()
            return
        }
        Thread {
            activity.runOnUiThread {
                val webPage = WXWebpageObject()
                webPage.webpageUrl = shareUrl //分享url
                val msg = WXMediaMessage(webPage)
                msg.title = title
                msg.description = desc
                val req = SendMessageToWX.Req()
                req.transaction = buildTransaction("webpage")
                req.message = msg
                req.scene = if (friendsCircle) SendMessageToWX.Req.WXSceneTimeline else SendMessageToWX.Req.WXSceneSession
                iwxapi.sendReq(req)
            }
        }.start()
    }

    private fun buildTransaction(type: String?): String {
        return if (type == null) System.currentTimeMillis().toString() else type + System.currentTimeMillis()
    }
}